package com.iss.card.view.account;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.iss.card.R;
import com.iss.card.common.FunctionUtil;
import com.iss.card.view.overrideview.SlideGallery;
import com.iss.card.view.overrideview.SlideGallery.IGalleryScrollEventListener;

public class AccountFirstUse extends Fragment {
	private final static String TAG = AccountFirstUse.class.getSimpleName();
	private Activity mContext;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mContext = activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.app_feature_guide, null);
		SlideGallery a = (SlideGallery) v
				.findViewById(R.id.gallery_app_feature_guide);
		a.setUnselectedAlpha(1.0f);
		List<Integer> contents = new ArrayList<Integer>();
		contents.add(R.drawable.app_feature_01);
		contents.add(R.drawable.app_feature_02);
		contents.add(R.drawable.app_feature_03);
		contents.add(R.drawable.app_feature_04);
		a.setAdapter(new FeatureGuideAdapter(contents));
		a.setScrollEventListener(new IGalleryScrollEventListener() {

			@Override
			public void onScrollEvent(int keyCode, AdapterView<?> parent,
					int position) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_DPAD_LEFT:
					if (position == 0) {
						// try to scroll the view before the first one.
						mContext.finish();
					}
					break;
				case KeyEvent.KEYCODE_DPAD_RIGHT:
					if (position == (parent.getCount() - 1)) {
						// try to scroll the view after the last one.
						mContext.finish();
					}
					break;
				default:
					break;
				}
			}
		});

		return v;
	}

	private final class FeatureGuideAdapter extends BaseAdapter {
		private List<Integer> mContents = new ArrayList<Integer>();

		public FeatureGuideAdapter(List<Integer> data) {
			mContents.addAll(data);
		}

		@Override
		public int getCount() {
			return mContents.size();
		}

		@Override
		public Object getItem(int position) {
			return mContents.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.app_feature_item, null);
				holder.mIVContent = (ImageView) convertView
						.findViewById(R.id.iv_app_feature_item);
				holder.mIVContent
						.setLayoutParams(new RelativeLayout.LayoutParams(
								LayoutParams.MATCH_PARENT,
								LayoutParams.MATCH_PARENT));
				convertView.setTag(holder);
			}

			holder = (ViewHolder) convertView.getTag();
			final int resId = mContents.get(position);
			try {
				holder.mIVContent.setImageResource(resId);
			} catch (OutOfMemoryError e) {
				e.printStackTrace();
				FunctionUtil.recycleMemory();
			}

			return convertView;
		}

		private final class ViewHolder {
			private ImageView mIVContent;
		}
	}
}
